
Date.prototype.format = function (formatString) {
  const pad = (number) => (number < 10 ? '0' + number : number);

  return formatString.replace(/YYYY|MM|DD|HH|mm|ss|fff/g, (match) => {
    switch (match) {
      case 'YYYY':
        return this.getFullYear();
      case 'MM':
        return pad(this.getMonth() + 1);
      case 'DD':
        return pad(this.getDate());
      case 'HH':
        return pad(this.getHours());
      case 'mm':
        return pad(this.getMinutes());
      case 'ss':
        return pad(this.getSeconds());
      case 'fff':
        return pad(this.getMilliseconds());
      default:
        return match;
    }
  });
};

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {string} time
 * @returns {string}
 */
export function formatTime(time) {
  if (!time) return ''
  return new Date(time).format('YYYY-MM-DD HH:mm:ss')
}

/**
 * @param {number} order_status
 * @returns {string}
 */
export function formatStatus(order_status) {
  return ['', '待提交', '待设计', '待确认', '待修改', '已完成'][order_status]
}
export function statusTag(order_status) {
  return ['', 'info', 'primary', 'warning', 'danger', 'success'][order_status]
}

export function findParentNode(rootArray, currentName) {
  if (!rootArray || !currentName) return null;

  function traverse(node, parent) {
    // 如果找到了当前节点，返回它的父节点
    if (node.name === currentName) {
      return parent;
    }

    // 如果当前节点有子节点，遍历它们
    if (node.children) {
      for (let child of node.children) {
        const result = traverse(child, node);
        if (result) return result; // 如果找到了，立即返回结果
      }
    }

    // 如果没有找到，返回null
    return null;
  }

  // 遍历根节点数组
  for (let node of rootArray) {
    const result = traverse(node, null);
    if (result) return result; // 如果找到了，立即返回结果
  }

  // 如果没有找到，返回null
  return null;
}

// 拍平树
export function flattenTree(tree) {
  let result = [];

  function flatten(node) {
    if (!node) return;

    // 将当前节点添加到结果数组中
    result.push(node);

    // 如果当前节点有 children，递归处理每个 child
    if (node.children && node.children.length > 0) {
      node.children.forEach(child => flatten(child));
    }
  }

  // 遍历树中的每个节点
  tree.forEach(node => flatten(node));

  return result;
}
